﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace LetsDoIt.Controls
{
	public partial class CheckBoxImage : UserControl
	{
		public CheckBoxImage()
		{
			InitializeComponent();
		}

		private Image checkedImage;
		public Image CheckedImage
		{
			get { return checkedImage; }
			set
			{
				checkedImage = value;
				this.Invalidate();
			}
		}

		private Image uncheckedImage;
		public Image UncheckedImage
		{
			get { return uncheckedImage; }
			set 
			{ 
				uncheckedImage = value;
				this.Invalidate();
			}
		}

		private bool bChecked;
		public bool Checked
		{
			get
			{
				return bChecked;
			}
			set
			{
				if (bChecked != value)
				{
					bChecked = value;
					if (bChecked)
						checkState = CheckState.Checked;
					else
						checkState = CheckState.Unchecked;

					this.Invalidate();
				}
			}
		}

		private CheckState checkState;
		public System.Windows.Forms.CheckState CheckState
		{
			get
			{
				return checkState;
			}
			set
			{
				if (checkState != value)
				{
					checkState = value;
					if (checkState == CheckState.Checked)
						bChecked = true;
					else if (checkState == CheckState.Unchecked)
						bChecked = false;
					else
						bChecked = false;

					this.Invalidate();
					OnCheckStateChanged(null);
				}
			}
		}

		public event System.EventHandler CheckStateChanged;

		public new event System.EventHandler GotFocus;
		public new event System.EventHandler LostFocus;

		protected virtual void OnCheckStateChanged(System.EventArgs e)
		{
			//Raise event
			if (CheckStateChanged != null)
				CheckStateChanged(this, e);
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			ImageAttributes attr = new ImageAttributes();
			attr.SetColorKey(_transparentColor, _transparentColor);
			if (bChecked)
			{
				if (checkedImage!=null)
					e.Graphics.DrawImage(checkedImage, this.ClientRectangle, 0, 0, checkedImage.Width,
									 checkedImage.Height, GraphicsUnit.Pixel, attr);
			}
			else
			{
				if (uncheckedImage != null)
					e.Graphics.DrawImage(uncheckedImage, this.ClientRectangle, 0, 0, uncheckedImage.Width,
									 uncheckedImage.Height, GraphicsUnit.Pixel, attr);
			}
		}


		protected override void OnPaintBackground(PaintEventArgs e)
		{
			//do nothing
			e.Graphics.Clear(Parent.BackColor);
		}

		private bool raiseEvent;
		protected override void OnMouseDown(MouseEventArgs e)
		{
			this.Focus();

			raiseEvent = false;
			if (bChecked)
				checkState = CheckState.Unchecked;
			else
				checkState = CheckState.Checked;

			bChecked = !bChecked;
			
			raiseEvent = true;
			this.Invalidate();
		}

		protected override void OnMouseUp(MouseEventArgs e)
		{
			if (raiseEvent)
				OnCheckStateChanged(null);

			raiseEvent = false;
			base.OnMouseUp(e);
		}

		protected override void OnGotFocus(EventArgs e)
		{
			if (this.GotFocus != null)
				this.GotFocus(this, null);
		}

		protected override void OnLostFocus(EventArgs e)
		{
			if (this.LostFocus != null)
				this.LostFocus(this, null);
		}

		#region Transparent Color
		Color _transparentColor;
		/// <summary>
		/// Sets or gets transparent color for an Image. The default color is 255, 0, 255.
		/// </summary>
		public Color TransparentColor
		{
			get
			{
				return _transparentColor;
			}
			set
			{
				_transparentColor = value;
				this.Invalidate();
			}
		}
		#endregion
	}
}
